package com.xckj.zuoliao.ui.activity;

import android.content.Intent;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xckj.zuoliao.R;
import com.xckj.zuoliao.bean.ListBean;
import com.xckj.zuoliao.databinding.ActivityMacaoListBinding;
import com.xckj.zuoliao.framework.BaseActivity;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.IListView;
import com.xckj.zuoliao.presenter.ListPresenter;
import com.xckj.zuoliao.ui.adapter.MacaoListAdapter;
import com.xckj.zuoliao.utils.SpUtil;
import com.xckj.zuoliao.view.CustomDialogView;

import java.util.List;

/**
 * @Author: 代码的搬运工
 * @Describe: 澳门列表
 * @Date: 2024/3/29 21:59
 */
public class MacaoListActivity extends BaseActivity<ActivityMacaoListBinding> implements IListView {
    private String areaType;
    @Override
    protected int setStatusBarColor() {
        return R.color.status_bar_green;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_macao_list;
    }

    @Override
    protected void initData() {
        dataBinding.viewTitleBar.tvTitle.setText("博彩平台");
         areaType = getIntent().getStringExtra(Constant.AREA_TYPE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ListPresenter listPresenter = new ListPresenter(this);
        listPresenter.getList(SpUtil.getString(this, Constant.ID), areaType);
    }

    private void initList(List<ListBean> listBeans) {
        dataBinding.list.setLayoutManager(new LinearLayoutManager(this));
        MacaoListAdapter listAdapter = new MacaoListAdapter(this);
        dataBinding.list.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener((view, position) -> {
            startActivity(new Intent(this, MacaoDetailsActivity.class).putExtra(Constant.COLUM, listBeans.get(position).colum));
        });
        listAdapter.setData(listBeans);
    }

    @Override
    protected void setListener() {
        dataBinding.viewTitleBar.ivBack.setOnClickListener(view -> {
            finish();
        });
    }

    @Override
    public void success(List<ListBean> listBeans) {
        initList(listBeans);
    }

    @Override
    public void failed(String message) {
        if ("用户不存在，请重新登录".equals(message)) {
            showDialog(message, 1);
        } else if ("账户已冻结，无法访问".equals(message)) {
            showDialog(message, 2);
        } else {
            showMsg(message);
        }
    }

    private void showDialog(String message, int type) {
        new CustomDialogView(this).setTitle("温馨提示").setMessage(message).setSureText("确定").setCancelable(false).showOneButton(true).setSureOnClickListener(view -> {
            if (1 == type) {
                SpUtil.clear(this);
                startActivity(new Intent(this, LoginActivity.class));
                finish();
            } else {
                finish();
            }
        }).build().show();
    }
}
